package com.hans.Utils;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ContactUtils {

    public static List<String> getContactAddress(Context context, String contactId){
        /*查找联系人地址*/
        String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
        String[] addrWhereParams = new String[]{contactId,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
        Cursor addrCur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, addrWhere, addrWhereParams, null);
        List<String> addressList = new ArrayList<>();
        while(addrCur.moveToNext()) {
            String address = "";
            String poBox = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
            String type = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
            String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
            String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
            if(country != null) address += country;
            String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
            if(state != null) address += state;
            String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
            if(city != null) address += city;
            String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
            if(street != null) address += street;
            addressList.add(address);
        }
        addrCur.close();
        return addressList;
    }

    /**
     * 获取联系人的ID
     * @param context
     * @param contactUri 一般是跳转到联系人页面点击选取了一个联系人后通过data.getData()得到的Uri
     * @return
     */
    public static String getContactId(Context context,Uri contactUri){
        Cursor cursor = context.getContentResolver().query(contactUri,null,null,null,null);
        if(cursor != null){
            cursor.moveToFirst();
            return cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        }
        cursor.close();
        return null;
    }

    /**
     * 得到指定ID的联系人的所有手机号码
     * @param context
     * @param contactId 联系人ID，这个联系人ID是通过上面函数getContactId查询得到的
     * @return
     */
    public static List<String> getPhoneNums(Context context,String contactId){
        return getContactInfo(context,ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,contactId,
                ContactsContract.CommonDataKinds.Phone.NUMBER);
    }

    /**
     * 得到指定ID联系人的所有邮箱
     * @param context
     * @param contactId 联系人ID，这个联系人ID是通过上面函数getContactId查询得到的
     * @return
     */
    public static List<String> getEmails(Context context,String contactId){
        return getContactInfo(context, ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID,contactId,
                ContactsContract.CommonDataKinds.Email.DATA);
    }

    /**
     * 从指定的Uri中查询指定id用户的指定字段的数据
     * @param context
     * @param contentUri
     * @param contactIdKey
     * @param contactId
     * @param keyToGetData
     * @return
     */
    public static List<String> getContactInfo(Context context, Uri contentUri, String contactIdKey, String contactId, String keyToGetData){
        Cursor infoCursor = context.getContentResolver().query(contentUri,null,contactIdKey + " = "+contactId,null,null);
        List<String> dataList = new ArrayList<>();
        while(infoCursor != null && infoCursor.moveToNext()){
            String phoneNum = infoCursor.getString(infoCursor.getColumnIndex(keyToGetData));
            dataList.add(phoneNum);
        }
        infoCursor.close();
        return dataList;
    }

    /**
     * 在Data表中查询指定联系人ID的指定类型和指定字段名的数据
     * @param context
     * @param contactId
     * @param mimetype
     * @param keyNames
     * @return
     */
    public static List<String> getContactInfo(Context context,String contactId, String mimetype,List<String> keyNames) {
        if (mimetype.isEmpty() || keyNames.isEmpty())return null;
        Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,null, ContactsContract.Data.RAW_CONTACT_ID + " = " + contactId, null, ContactsContract.Data.RAW_CONTACT_ID);
        List<String> resultList = new ArrayList<String>();
        while (cursor.moveToNext()) {
            // 取得mimetype类型
            if (mimetype.equals(cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE)))) {
                for (int i = 0; i < keyNames.size(); i++)
                    resultList.add(cursor.getString(cursor.getColumnIndex(keyNames.get(i))));
            }
        }
        return resultList;
    }

    /**
     * 向Data表中插入一个联系人的名字数据
     * @param context
     * @param rawContactId 联系人的ID,如果是新插入一个联系人，可以先插入一个空的ContentValue来返回一个long型的ID
     * @param name
     */
    public void insertNameToRawContact(Context context,long rawContactId,String name){
        ContentValues values = new ContentValues();
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
        context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
    }
    /**
     * 向Data表中插入一个联系人的手机数据
     * @param context
     * @param rawContactId 联系人的ID,如果是新插入一个联系人，可以先插入一个空的ContentValue来返回一个long型的ID
     * @param phone
     */
    public void insertPhoneToRawContact(Context context,long rawContactId,String phone){
        ContentValues values = new ContentValues();
        values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID,rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone);
        values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
        context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
    }
    /**
     * 向Data表中插入一个联系人的邮箱数据
     * @param context
     * @param rawContactId 联系人的ID,如果是新插入一个联系人，可以先插入一个空的ContentValue来返回一个long型的ID
     * @param email
     */
    public void insertEmailToRawContact(Context context,long rawContactId,String email){
        ContentValues values = new ContentValues();
        values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID,rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.Email.DATA, email);
        values.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME);
        context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
    }

    /**
     * 插入一个联系人信息到电话簿数据库
     * @param context
     * @param name
     * @param phone
     * @param email
     */
   public void addContactToLocal(Context context, String name,String phone,String email) {
        ContentValues values = new ContentValues();
        // 首先向RawContacts.CONTENT_URI执行一个空值插入，目的是获取系统返回的rawContactId
        Uri rawContactUri = context.getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);
        long rawContactId = ContentUris.parseId(rawContactUri);

        // 往data表插入姓名数据
       if(!TextUtils.isEmpty(name)){
           insertNameToRawContact(context,rawContactId,name);
       }
        // 往data表插入电话数据
       if(!TextUtils.isEmpty(name)){
            insertPhoneToRawContact(context,rawContactId,phone);
       }
        // 往data表插入Email数据
       if(!TextUtils.isEmpty(name)){
            insertEmailToRawContact(context,rawContactId,email);
       }
    }

    /**
     * 跳转到新建联系人界面
     * @param context
     * @param name
     * @param phones
     * @param emails
     * @param address
     */
    public void JmpToAddContactActivity(Context context, String name,List<String> phones,List<String> emails,String address) {
        Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
        intent.setType("vnd.android.cursor.item/person");
        intent.setType("vnd.android.cursor.item/contact");
        intent.setType("vnd.android.cursor.item/raw_contact");
        if(!TextUtils.isEmpty(name)) intent.putExtra(ContactsContract.Intents.Insert.NAME,name);
        if(phones != null && phones.size() > 0) try {
            intent.putExtra(ContactsContract.Intents.Insert.PHONE,phones.get(0));
            intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE,phones.get(1));
            intent.putExtra(ContactsContract.Intents.Insert.TERTIARY_PHONE,phones.get(2));
        }catch (Exception e){}

        if(phones != null && emails.size() > 0) try {
            intent.putExtra(ContactsContract.Intents.Insert.EMAIL,emails.get(0));
            intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_EMAIL,emails.get(1));
            intent.putExtra(ContactsContract.Intents.Insert.TERTIARY_EMAIL,emails.get(2));
        }catch (Exception e){}

        if(!TextUtils.isEmpty(address)) {
            intent.putExtra(Contacts.Intents.Insert.POSTAL,address);
            intent.putExtra(Contacts.Intents.Insert.POSTAL_TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME);
        }
        context.startActivity(intent);
    }


    /**
     * 通过已经废弃的Contacts类来查询联系人信息， 包括邮箱，IM，组织，电话，地址，查询结果通过多行显示
     * @param context
     * @param contactId 联系人ID
     * @return
     */
    public static String getContactInfo(Context context,String contactId){
        Cursor contactMethods = context.getContentResolver().query(Contacts.ContactMethods.CONTENT_URI, null,Contacts.ContactMethods.PERSON_ID + " = "+contactId,null, Contacts.ContactMethods.DEFAULT_SORT_ORDER);
        String result = "";
        while (contactMethods.moveToNext()) {
            int myKind = contactMethods.getInt(contactMethods.getColumnIndex(Contacts.ContactMethods.KIND));
            if (Contacts.KIND_EMAIL == myKind) {
                result = result + "\nemail::"+contactMethods.getString(contactMethods.getColumnIndex(Contacts.ContactMethods.DATA));
            }else if(Contacts.KIND_IM == myKind){
                result = result + "\nim::"+contactMethods.getString(contactMethods.getColumnIndex(Contacts.ContactMethods.DATA)) ;
            }else if(Contacts.KIND_ORGANIZATION == myKind){
                result = result + "\norg::"+contactMethods.getString(contactMethods.getColumnIndex(Contacts.ContactMethods.DATA)) ;
            }else if(Contacts.KIND_PHONE == myKind){
                result = result + "\nphone::"+contactMethods.getString(contactMethods.getColumnIndex(Contacts.ContactMethods.DATA));
            }else if(Contacts.KIND_POSTAL == myKind){
                result = result + "\naddress::"+contactMethods.getString(contactMethods.getColumnIndex(Contacts.ContactMethods.DATA));
            }
        }
        contactMethods.close();
        return result;
    }

    /**
     * 需要两层分类就能获取到数值的就用这个 比如，名称信息-->显示名称
     *
     * @param contactId
     * @param mimetype
     * @param keyName
     * @return
     */
    public static String getContactInfo(Context context,String contactId, String mimetype, String keyName) {
        if (mimetype.isEmpty() || keyName.isEmpty()) return null;
        Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,null, ContactsContract.Data.RAW_CONTACT_ID + " = " + contactId, null, ContactsContract.Data.RAW_CONTACT_ID);
        while (cursor.moveToNext()) {
            // 取得mimetype类型
            if (mimetype.equals(cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE)))) {
                return cursor.getString(cursor.getColumnIndex(keyName));
            }
        }
        return null;
    }




    /**
     * 对于MIMETYPE分类后的数据的子数据只有一种值的情况适合这个方法。比如说MIMETYPE为名字信息的时候，GIVEM_NAME
     * 的取值就只有一种，它没有对应其他的一些内容。即不需要根据这个子分类再取查询一张表来获取信息。比如，MIMETYPE
     * 为电话信息时，子分类有行动电话，家庭电话，公司电话等。但是这个行动电话等不是直接的行动电话内容，而它还是一个
     * 类型。你还需要通过这个类型查询行动电话里面的号码，内容等等其他信息。 总而言之需要三层分类才能获取到数值的就用这个，比如，
     * 电话信息-->行动电话-->电话号码
     *
     * @param contactId
     * @param mimetype
     * @param keyNames
     * @return
     */
    public static String getContactInfo(Context context,String contactId, String mimetype,int subType, String subTypeColumnStr, String keyNames) {
        if (mimetype.isEmpty() || keyNames.isEmpty())return null;
        Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,null, ContactsContract.Data.RAW_CONTACT_ID + " = " + contactId, null, ContactsContract.Data.RAW_CONTACT_ID); // 查询这个用户ID的所有数据
        while (cursor.moveToNext()) {
            if (mimetype.equals(cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE)))) { // 只对这些数据中指定类型的数据，比如电话，邮箱进行处理
                cursor.getString(cursor.getColumnIndex(keyNames));
                if (subType == cursor.getInt(cursor.getColumnIndex(subTypeColumnStr))) // 只对这中类型的数据中的子类型，比如工作电话，家庭电话内容进行处理
                    return cursor.getString(cursor.getColumnIndex(keyNames));
            }
        }
        return null;
    }


    /**
     * 获取所有联系人的所有信息并组装成JSON字符串
     * @param context
     * @return
     * @throws JSONException
     */
    public static String getAllContactInfoToJsonStr(Context context) throws JSONException {
        // 获得通讯录信息 ，URI是ContactsContract.Contacts.CONTENT_URI
        JSONObject contactData = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        String mimetype = "";
        int oldrid = -1;
        int contactId = -1;
        Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,null, null, null, ContactsContract.Data.RAW_CONTACT_ID);
        int numm = 0;
        while (cursor.moveToNext()) {
            contactId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
            if (oldrid != contactId) {
                contactData.put("contact" + numm, jsonObject);
                numm++;
                oldrid = contactId;
            }

            // 取得mimetype类型
            mimetype = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
            // 获得通讯录中每个联系人的ID
            // 获得通讯录中联系人的名字

            if (ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE.equals(mimetype)) {
                // String display_name = cursor.getString(cursor.getColumnIndex(StructuredName.DISPLAY_NAME));
                String prefix = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
                jsonObject.put("prefix", prefix);
                String firstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                jsonObject.put("firstName", firstName);
                String middleName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
                jsonObject.put("middleName", middleName);
                String lastname = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                jsonObject.put("lastname", lastname);
                String suffix = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX));
                jsonObject.put("suffix", suffix);
                String phoneticFirstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_FAMILY_NAME));
                jsonObject.put("phoneticFirstName", phoneticFirstName);
                String phoneticMiddleName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_MIDDLE_NAME));
                jsonObject.put("phoneticMiddleName", phoneticMiddleName);
                String phoneticLastName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_GIVEN_NAME));
                jsonObject.put("phoneticLastName", phoneticLastName);
            }
            // 获取电话信息
            if (ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
                // 取出电话类型
                int phoneType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                // 手机
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
                    String mobile = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("mobile", mobile);
                }
                // 住宅电话
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_HOME) {
                    String homeNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("homeNum", homeNum);
                }
                // 单位电话
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_WORK) {
                    String jobNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("jobNum", jobNum);
                }
                // 单位传真
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK) {
                    String workFax = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("workFax", workFax);
                }
                // 住宅传真
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME) {
                    String homeFax = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("homeFax", homeFax);
                }
                // 寻呼机
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_PAGER) {
                    String pager = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("pager", pager);
                }
                // 回拨号码
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK) {
                    String quickNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("quickNum", quickNum);
                }
                // 公司总机
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN) {
                    String jobTel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("jobTel", jobTel);
                }
                // 车载电话
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_CAR) {
                    String carNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("carNum", carNum);
                }
                // ISDN
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_ISDN) {
                    String isdn = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("isdn", isdn);
                }
                // 总机
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MAIN) {
                    String tel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("tel", tel);
                }
                // 无线装置
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_RADIO) {
                    String wirelessDev = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("wirelessDev", wirelessDev);
                }
                // 电报
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_TELEX) {
                    String telegram = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("telegram", telegram);
                }
                // TTY_TDD
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD) {
                    String tty_tdd = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("tty_tdd", tty_tdd);
                }
                // 单位手机
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE) {
                    String workMobile = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("workMobile", workMobile);
                }
                // 单位寻呼机
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER) {
                    String workPager = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("workPager", workPager);
                }
                // 助理
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT) {
                    String assistantNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("assistantNum", assistantNum);
                }
                // 彩信
                if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MMS) {
                    String mms = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    jsonObject.put("mms", mms);
                }
            }
            // }
            // 查找email地址
            if (ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
                // 取出邮件类型
                int emailType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                // 住宅邮件地址
                if (emailType == ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM) {
                    String homeEmail = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    jsonObject.put("homeEmail", homeEmail);
                }

                // 住宅邮件地址
                else if (emailType == ContactsContract.CommonDataKinds.Email.TYPE_HOME) {
                    String homeEmail = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    jsonObject.put("homeEmail", homeEmail);
                }
                // 单位邮件地址
                if (emailType == ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM) {
                    String jobEmail = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    jsonObject.put("jobEmail", jobEmail);
                }

                // 单位邮件地址
                else if (emailType == ContactsContract.CommonDataKinds.Email.TYPE_WORK) {
                    String jobEmail = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    jsonObject.put("jobEmail", jobEmail);
                }
                // 手机邮件地址
                if (emailType == ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM) {
                    String mobileEmail = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    jsonObject.put("mobileEmail", mobileEmail);
                }

                // 手机邮件地址
                else if (emailType == ContactsContract.CommonDataKinds.Email.TYPE_MOBILE) {
                    String mobileEmail = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    jsonObject.put("mobileEmail", mobileEmail);
                }
            }
            // 查找event地址
            if (ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE.equals(mimetype)) {
                // 取出时间类型
                int eventType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE));
                // 生日
                if (eventType == ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY) {
                    String birthday = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
                    jsonObject.put("birthday", birthday);
                }
                // 周年纪念日
                if (eventType == ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY) {
                    String anniversary = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
                    jsonObject.put("anniversary", anniversary);
                }
            }
            // 即时消息
            if (ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
                // 取出即时消息类型
                int protocal = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL));
                if (ContactsContract.CommonDataKinds.Im.TYPE_CUSTOM == protocal) {
                    String workMsg = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
                    jsonObject.put("workMsg", workMsg);
                }

                else if (ContactsContract.CommonDataKinds.Im.PROTOCOL_MSN == protocal) {
                    String workMsg = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
                    jsonObject.put("workMsg", workMsg);
                }
                if (ContactsContract.CommonDataKinds.Im.PROTOCOL_QQ == protocal) {
                    String instantsMsg = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
                    jsonObject.put("instantsMsg", instantsMsg);
                }
            }
            // 获取备注信息
            if (ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
                String remark = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
                jsonObject.put("note", remark);
            }
            // 获取昵称信息
            if (ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
                String nickName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
                jsonObject.put("nickName", nickName);
            }
            // 获取组织信息
            if (ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
                // 取出组织类型
                int orgType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TYPE));
                // 单位
                if (orgType == ContactsContract.CommonDataKinds.Organization.TYPE_CUSTOM) {
                    // if (orgType == Organization.TYPE_WORK) {
                    String company = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
                    jsonObject.put("company", company);
                    String jobTitle = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
                    jsonObject.put("jobTitle", jobTitle);
                    String department = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DEPARTMENT));
                    jsonObject.put("department", department);
                }
            }
            // 获取网站信息
            if (ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE.equals(mimetype)) {
                // 取出组织类型
                int webType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE));
                // 主页
                if (webType == ContactsContract.CommonDataKinds.Website.TYPE_CUSTOM) {
                    String home = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
                    jsonObject.put("custom", home);
                }
                // 主页
                else if (webType == ContactsContract.CommonDataKinds.Website.TYPE_HOME) {
                    String home = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
                    jsonObject.put("home", home);
                }

                // 个人主页
                if (webType == ContactsContract.CommonDataKinds.Website.TYPE_HOMEPAGE) {
                    String homePage = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
                    jsonObject.put("homePage", homePage);
                }
                // 工作主页
                if (webType == ContactsContract.CommonDataKinds.Website.TYPE_WORK) {
                    String workPage = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
                    jsonObject.put("workPage", workPage);
                }
            }
            // 查找通讯地址
            if (ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)) {
                // 取出邮件类型
                int postalType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
                // 单位通讯地址
                if (postalType == ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK) {
                    String street = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                    jsonObject.put("street", street);
                    String ciry = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                    jsonObject.put("ciry", ciry);
                    String box = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                    jsonObject.put("box", box);
                    String area = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD));
                    jsonObject.put("area", area);
                    String state = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                    jsonObject.put("state", state);
                    String zip = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                    jsonObject.put("zip", zip);
                    String country = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                    jsonObject.put("country", country);
                }
                // 住宅通讯地址
                if (postalType == ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME) {
                    String homeStreet = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                    jsonObject.put("homeStreet", homeStreet);
                    String homeCity = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                    jsonObject.put("homeCity", homeCity);
                    String homeBox = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                    jsonObject.put("homeBox", homeBox);
                    String homeArea = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD));
                    jsonObject.put("homeArea", homeArea);
                    String homeState = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                    jsonObject.put("homeState", homeState);
                    String homeZip = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                    jsonObject.put("homeZip", homeZip);
                    String homeCountry = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                    jsonObject.put("homeCountry", homeCountry);
                }
                // 其他通讯地址
                if (postalType == ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER) {
                    String otherStreet = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                    jsonObject.put("otherStreet", otherStreet);
                    String otherCity = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                    jsonObject.put("otherCity", otherCity);
                    String otherBox = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                    jsonObject.put("otherBox", otherBox);
                    String otherArea = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD));
                    jsonObject.put("otherArea", otherArea);
                    String otherState = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                    jsonObject.put("otherState", otherState);
                    String otherZip = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                    jsonObject.put("otherZip", otherZip);
                    String otherCountry = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                    jsonObject.put("otherCountry", otherCountry);
                }
            }
        }
        cursor.close();
        Log.i("SamLog", contactData.toString());
        return contactData.toString();
    }
}
